﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

namespace Gtkj.CMS.BLL
{
    public class RxjhHelper
    {
        public RxjhHelper()
        {
        }

        public ItmeSClass GetBag(string username, string serverdb = "0", string serverid = "d1")
        {
            Dictionary<int, 物品类> ItmeList = null;
            string ServerId = serverid;
            string UserName = username;
            string SelectDb = serverdb;
            string DataName = "TBL_XWWL_Char";
            string DataWere = "FLD_ITEM,FLD_ID,FLD_JOB,FLD_ZX";
            string DataWere2 = "FLD_ITEM";
            string DataKey = "FLD_NAME";
            int 物品数 = 36;
            if (SelectDb == "0")
            {
                DataName = "TBL_XWWL_Char";
                DataWere = "FLD_ITEM,FLD_ID,FLD_JOB,FLD_ZX";
                DataWere2 = "FLD_ITEM";
                DataKey = "FLD_NAME";
                物品数 = 36;
            }
            else if (SelectDb == "1")
            {
                DataName = "TBL_XWWL_Char";
                DataWere = "FLD_WEARITEM,FLD_ID,FLD_JOB,FLD_ZX";
                DataWere2 = "FLD_WEARITEM";
                DataKey = "FLD_NAME";
                物品数 = 15;
            }
            else if (SelectDb == "2")
            {
                DataName = "TBL_XWWL_Warehouse";
                DataWere = "FLD_ITEM,FLD_ID";
                DataWere2 = "FLD_ITEM";
                DataKey = "FLD_NAME";
                物品数 = 60;
            }
            else if (SelectDb == "3")
            {
                DataName = "TBL_XWWL_PublicWarehouse";
                DataWere = "FLD_ITEM,FLD_ID";
                DataWere2 = "FLD_ITEM";
                DataKey = "FLD_ID";
                物品数 = 60;
            }
            string sql = string.Format("select {3} from {2} WHERE {1} ='{0}'", new object[] { UserName, DataKey, DataName, DataWere });
            DataTable table1 = DBA.GetDBToDataTable(sql, "rxjhgame", ServerId);
            if (table1 == null)
            {
                throw new ArgumentException("提示：错误没有这个人物！数据库可能有问题");
            }
            if (table1.Rows.Count == 0)
            {
                throw new ArgumentException("提示：错误没有这个人物！");
            }
            ItmeList = new Dictionary<int, 物品类>();
            ItmeSClass ItmeSC = new ItmeSClass();
            byte[] items = (byte[])table1.Rows[0][DataWere2];
            for (int i = 0; i < 物品数; i++)
            {
                if ((int)items.Length >= i * 73 + 73)
                {
                    byte[] 单个物品 = new byte[73];
                    try
                    {
                        Buffer.BlockCopy(items, i * 73, 单个物品, 0, 73);
                        物品类 物品 = new 物品类(单个物品);
                        ItmeList.Add(i, 物品);
                        ItmeClass Itme = new ItmeClass()
                        {
                            ID = i,
                            FLD_ID = BitConverter.ToInt64(物品.物品全局ID, 0),
                            FLD_ItmeID = BitConverter.ToInt32(物品.物品ID, 0),
                            FLD_NAME = 物品.物品名称,
                            FLD_Type = 物品.物品类型,
                            FLD_Suliang = BitConverter.ToInt32(物品.物品数量, 0),
                            FLD_MAGIC0 = 物品.FLD_MAGIC0,
                            FLD_MAGIC1 = 物品.FLD_MAGIC1,
                            FLD_MAGIC2 = 物品.FLD_MAGIC2,
                            FLD_MAGIC3 = 物品.FLD_MAGIC3,
                            FLD_MAGIC4 = 物品.FLD_MAGIC4,
                            FLD_CjFh = 物品.FLD_FJ_初级附魂,
                            FLD_ZjFh = 物品.FLD_FJ_中级附魂,
                            FLD_JingHua = 物品.FLD_FJ_进化,
                            FLD_Bind = 物品.物品绑定
                        };
                        ItmeSC.ItmeList.Add(Itme);
                        if (BitConverter.ToInt32(物品.物品ID, 0) == 0)
                        {
                            ItmeSC.Kwei++;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new ArgumentException(string.Concat("提示：数据错误1！", exception.Message));
                    }
                    ItmeSC.FLD_ID = table1.Rows[0]["FLD_ID"].ToString();
                    ItmeSC.FLD_Name = UserName;
                    if ((SelectDb == "2" ? false : SelectDb != "3"))
                    {
                        ItmeSC.FLD_JOB = table1.Rows[0]["FLD_JOB"].ToString();
                        ItmeSC.FLD_ZX = table1.Rows[0]["FLD_ZX"].ToString();
                    }
                    string sqlid = string.Format("select FLD_ID,FLD_ONLINE,FLD_SEX from [TBL_ACCOUNT] where FLD_ID='{0}'", ItmeSC.FLD_ID);
                    DataTable tableid = DBA.GetDBToDataTable(sqlid, "rxjhaccount", ServerId);
                    ItmeSC.FLD_ONLINE = tableid.Rows[0]["FLD_ONLINE"].ToString();
                    tableid.Dispose();
                }
            }
            table1.Dispose();
            return ItmeSC;
        }

        public dynamic InsertItem(string username, int weizi, int itemid)
        {
            object obj;
            try
            {
                string ServerId = "d1";
                string UserName = username;
                int weiz = weizi;
                long WordId = (long)0;
                int ItmeId = itemid;
                int SunXing0 = 0;
                int SunXing1 = 0;
                int SunXing2 = 0;
                int SunXing3 = 0;
                int SunXing4 = 0;
                int CjFh = 0;
                int ZjFh = 0;
                int JingHua = 0;
                int ItmeShuling = 1;
                bool ItmeBind = false;
                string 银行数据 = "550088081";
                string SelectDb = "0";
                string DataName = "TBL_XWWL_Char";
                string DataWere = "FLD_ITEM,FLD_ID,FLD_JOB,FLD_ZX";
                string DataWere2 = "FLD_ITEM";
                string DataKey = "FLD_NAME";
                int 物品数 = 36;
                if (SelectDb == "0")
                {
                    DataName = "TBL_XWWL_Char";
                    DataWere = "FLD_ITEM,FLD_ID,FLD_JOB,FLD_ZX";
                    DataWere2 = "FLD_ITEM";
                    DataKey = "FLD_NAME";
                    物品数 = 36;
                }
                else if (SelectDb == "1")
                {
                    DataName = "TBL_XWWL_Char";
                    DataWere = "FLD_WEARITEM,FLD_ID,FLD_JOB,FLD_ZX";
                    DataWere2 = "FLD_WEARITEM";
                    DataKey = "FLD_NAME";
                    物品数 = 15;
                }
                else if (SelectDb == "2")
                {
                    DataName = "TBL_XWWL_Warehouse";
                    DataWere = "FLD_ITEM,FLD_ID";
                    DataWere2 = "FLD_ITEM";
                    DataKey = "FLD_NAME";
                    物品数 = 60;
                }
                else if (SelectDb == "3")
                {
                    DataName = "TBL_XWWL_PublicWarehouse";
                    DataWere = "FLD_ITEM,FLD_ID";
                    DataWere2 = "FLD_ITEM";
                    DataKey = "FLD_ID";
                    物品数 = 60;
                }
                string sql = string.Format("select {3} from {2} WHERE {1} ='{0}'", new object[] { UserName, DataKey, DataName, DataWere });
                DataTable table1 = DBA.GetDBToDataTable(sql, "rxjhgame", ServerId);
                if (table1 == null)
                {
                    obj = false;
                }
                else
                {
                    if (table1.Rows.Count == 0)
                    {
                        table1.Dispose();
                        throw new ArgumentException("提示：错误没有这个人物！");
                    }
                    物品类 物品 = new 物品类(new byte[73]);
                    if (WordId != (long)0)
                    {
                        银行数据 = string.Concat("更新物品 ", 银行数据);
                    }
                    else
                    {
                        WordId = long.Parse(DBA.GetDBValue_3("EXEC XWWL_GetItemSerial 1", "rxjhgame", ServerId).ToString());
                    }
                    物品.物品全局ID = BitConverter.GetBytes(WordId);
                    物品.物品ID = BitConverter.GetBytes(ItmeId);
                    物品.物品数量 = BitConverter.GetBytes(ItmeShuling);
                    物品.FLD_MAGIC0 = SunXing0;
                    物品.FLD_MAGIC1 = SunXing1;
                    物品.FLD_MAGIC2 = SunXing2;
                    物品.FLD_MAGIC3 = SunXing3;
                    物品.FLD_MAGIC4 = SunXing4;
                    物品.FLD_FJ_初级附魂 = CjFh;
                    物品.FLD_FJ_中级附魂 = ZjFh;
                    物品.FLD_FJ_进化 = JingHua;
                    物品.物品绑定 = ItmeBind;
                    byte[] items = (byte[])table1.Rows[0][DataWere2];
                    AdminClass.SetItmeLog(BitConverter.ToInt64(物品.物品全局ID, 0), 物品.物品名称, UserName, string.Concat(new object[] { SunXing0, "_", SunXing1, "_", SunXing2, "_", SunXing3, "_", SunXing4, " 初", CjFh, " 中", ZjFh, " 进", JingHua }), ServerId, 银行数据);
                    Buffer.BlockCopy(物品.物品_byte, 0, items, weiz * 73, 73);
                    AdminClass.UpItemsDate(ServerId, UserName, items, SelectDb);
                    ItmeSClass ItmeSC = new ItmeSClass()
                    {
                        FLD_ID = table1.Rows[0]["FLD_ID"].ToString(),
                        FLD_Name = UserName
                    };
                    if ((SelectDb == "2" ? false : SelectDb != "3"))
                    {
                        ItmeSC.FLD_JOB = table1.Rows[0]["FLD_JOB"].ToString();
                        ItmeSC.FLD_ZX = table1.Rows[0]["FLD_ZX"].ToString();
                    }
                    string sqlid = string.Format("select FLD_ID,FLD_ONLINE,FLD_SEX from [TBL_ACCOUNT] where FLD_ID='{0}'", ItmeSC.FLD_ID);
                    DataTable tableid = DBA.GetDBToDataTable(sqlid, "rxjhaccount", ServerId);
                    ItmeSC.FLD_ONLINE = tableid.Rows[0]["FLD_ONLINE"].ToString();
                    ItmeSC.FLD_SEX = tableid.Rows[0]["FLD_SEX"].ToString();
                    tableid.Dispose();
                    AdminClass.GetItemslist(ItmeSC, items, 36);
                    table1.Dispose();
                    obj = true;
                }
            }
            catch (Exception exception)
            {
                obj = false;
            }
            return obj;
        }
    }
}